home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / lpmud312.tar / lpmud312 / ed.c < prev    next >
C/C++ Source or Header  |  1991-12-12  |  55KB  |  2,282 lines

  1. /*
  2.  *  ed - standard editor
  3.  *  ~~
  4.  *    Authors: Brian Beattie, Kees Bot, and others
  5.  *
  6.  * Copyright 1987 Brian Beattie Rights Reserved.
  7.  * Permission to copy or distribute granted under the following conditions:
  8.  * 1). No charge may be made other than reasonable charges for reproduction.
  9.  * 2). This notice must remain intact.
  10.  * 3). No further restrictions may be added.
  11.  * 4). Except meaningless ones.
  12.  *
  13.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14.  *  TurboC mods and cleanup 8/17/88 RAMontante.
  15.  *  Further information (posting headers, etc.) at end of file.
  16.  *  RE stuff replaced with Spencerian version, sundry other bugfix+speedups
  17.  *  Ian Phillipps. Version incremented to "5".
  18.  * _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  19.  *
  20.  *  ********--->> INDENTATION ONLINE !!!! <<----------****************
  21.  *  Indentation added by Ted Gaunt (aka Qixx) paradox@mcs.anl.gov
  22.  *  help files added by Ted Gaunt
  23.  *  '^' added by Ted Gaunt
  24.  *  Note: this version compatible with v.3.0.34  (and probably all others too)
  25.  *  but i've only tested it on v.3 please mail me if it works on your mud
  26.  *  and if you like it!
  27.  */
  28.  
  29.  
  30. int    version = 5;    /* used only in the "set" function, for i.d. */
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #ifdef __STDC__
  35. #include <memory.h>
  36. #endif
  37. #include <sys/types.h>  /* need for netinet */
  38. /* #include <netinet/in.h> Included in comm.h */
  39. #include <ctype.h>
  40. /* Regexp is Henry Spencer's package. WARNING: regsub is modified to return
  41.  * a pointer to the \0 after the destination string, and this program refers
  42.  * to the "private" reganch field in the struct regexp.
  43.  */
  44. #include "lint.h"
  45. #include "regexp.h"
  46. #include "interpret.h"
  47. #include "object.h"
  48. #include "config.h"
  49. #include "exec.h"
  50. #include "comm.h"
  51. /*
  52.  *    #defines for non-printing ASCII characters
  53.  */
  54. #define NUL    0x00    /* ^@ */
  55. #define EOS    0x00    /* end of string */
  56. #define SOH    0x01    /* ^A */
  57. #define STX    0x02    /* ^B */
  58. #define ETX    0x03    /* ^C */
  59. #define EOT    0x04    /* ^D */
  60. #define ENQ    0x05    /* ^E */
  61. #define ACK    0x06    /* ^F */
  62. #define BEL    0x07    /* ^G */
  63. #define BS    0x08    /* ^H */
  64. #define HT    0x09    /* ^I */
  65. #define LF    0x0a    /* ^J */
  66. #define NL    '\n'
  67. #define VT    0x0b    /* ^K */
  68. #define FF    0x0c    /* ^L */
  69. #define CR    0x0d    /* ^M */
  70. #define SO    0x0e    /* ^N */
  71. #define SI    0x0f    /* ^O */
  72. #define DLE    0x10    /* ^P */
  73. #define DC1    0x11    /* ^Q */
  74. #define DC2    0x12    /* ^R */
  75. #define DC3    0x13    /* ^S */
  76. #define DC4    0x14    /* ^T */
  77. #define NAK    0x15    /* ^U */
  78. #define SYN    0x16    /* ^V */
  79. #define ETB    0x17    /* ^W */
  80. #define CAN    0x18    /* ^X */
  81. #define EM    0x19    /* ^Y */
  82. #define SUB    0x1a    /* ^Z */
  83. #define ESC    0x1b    /* ^[ */
  84. #define FS    0x1c    /* ^\ */
  85. #define GS    0x1d    /* ^] */
  86. /*#define RS    0x1e       ^^ */
  87. #define US    0x1f    /* ^_ */
  88. #define SP    0x20    /* space */
  89. #define DEL    0x7f    /* DEL*/
  90. #define ESCAPE  '\\'
  91.  
  92. #define TAB '\t'        /* added by Qixx for indentation */
  93. #define LB '{'
  94. #define RB '}'
  95. #define LC '('
  96. #define RC ')'
  97. #define LS '['
  98. #define RS ']'
  99. #define PP '\"'
  100. #define EOL '\0'
  101.  
  102.  
  103. #define TRUE    1
  104. #define FALSE    0
  105. #define ERR        -2
  106. #define FATAL        (ERR-1)
  107. #define CHANGED        (ERR-2)
  108. #define SET_FAIL    (ERR-3)
  109. #define SUB_FAIL    (ERR-4)
  110. #define MEM_FAIL    (ERR-5)
  111.  
  112.  
  113. #define    BUFFER_SIZE    2048    /* stream-buffer size:  == 1 hd cluster */
  114.  
  115. #define LINFREE    1    /* entry not in use */
  116. #define LGLOB    2       /* line marked global */
  117.  
  118. #define MAXLINE    512    /* max number of chars per line */
  119. #define MAXPAT    256    /* max number of chars per replacement pattern */
  120. #define MAXFNAME 256    /* max file name size */
  121.  
  122.  
  123. /**  Global variables  **/
  124.  
  125. extern struct program *current_prog;
  126.  
  127. struct    line {
  128.     int        l_stat;        /* empty, mark */
  129.     struct line    *l_prev;
  130.     struct line    *l_next;
  131.     char        l_buff[1];
  132. };
  133. typedef struct line    LINE;
  134.  
  135. extern struct object *command_giver;
  136. void set_prompt PROT((char *));
  137.  
  138. #ifndef toupper
  139. extern int toupper PROT((int));
  140. #endif
  141.  
  142. int doprnt PROT((int, int));
  143. int ins PROT((char *));
  144. int deflt PROT((int, int));
  145. static int strip_buff PROT((int line,char *buff2));
  146. static void print_help PROT((int arg));
  147. static void print_help2 PROT((void));
  148. static void count_blanks PROT((int line));
  149. static void _count_blanks PROT((char *str, int blanks));
  150.  
  151. #define P_DIAG        (command_giver->interactive->ed_buffer->diag)
  152. #define P_TRUNCFLG    (command_giver->interactive->ed_buffer->truncflg)
  153. #define P_NONASCII    (command_giver->interactive->ed_buffer->nonascii)
  154. #define P_NULLCHAR    (command_giver->interactive->ed_buffer->nullchar)
  155. #define P_TRUNCATED    (command_giver->interactive->ed_buffer->truncated)
  156. #define P_FNAME        (command_giver->interactive->ed_buffer->fname)
  157. #define P_FCHANGED    (command_giver->interactive->ed_buffer->fchanged)
  158. #define P_NOFNAME    (command_giver->interactive->ed_buffer->nofname)
  159. #define P_MARK        (command_giver->interactive->ed_buffer->mark)
  160. #define P_OLDPAT    (command_giver->interactive->ed_buffer->oldpat)
  161. #define P_LINE0        (command_giver->interactive->ed_buffer->Line0)
  162. #define P_LINE0        (command_giver->interactive->ed_buffer->Line0)
  163. #define P_CURLN        (command_giver->interactive->ed_buffer->CurLn)
  164. #define P_CURPTR    (command_giver->interactive->ed_buffer->CurPtr)
  165. #define P_LASTLN    (command_giver->interactive->ed_buffer->LastLn)
  166. #define P_LINE1        (command_giver->interactive->ed_buffer->Line1)
  167. #define P_LINE2        (command_giver->interactive->ed_buffer->Line2)
  168. #define P_NLINES    (command_giver->interactive->ed_buffer->nlines)
  169. #define P_SHIFTWIDTH    (command_giver->interactive->ed_buffer->shiftwidth)
  170. /* shiftwidth is meant to be a 4-bit-value that can be packed into an int
  171.    along with flags, therefore masks 0x1 ... 0x8 are reserved.           */
  172. #define P_FLAGS     (command_giver->interactive->ed_buffer->flags)
  173. #define NFLG_MASK    0x0010
  174. #define P_NFLG        ( P_FLAGS & NFLG_MASK )
  175. #define LFLG_MASK    0x0020
  176. #define P_LFLG        ( P_FLAGS & LFLG_MASK )
  177. #define PFLG_MASK    0x0040
  178. #define P_PFLG        ( P_FLAGS & PFLG_MASK )
  179. #define EIGHTBIT_MASK    0x0080
  180. #define P_EIGHTBIT    ( P_FLAGS & EIGHTBIT_MASK )
  181. #define AUTOINDFLG_MASK    0x0100
  182. #define P_AUTOINDFLG    ( P_FLAGS & AUTOINDFLG_MASK )
  183. #define EXCOMPAT_MASK    0x0200
  184. #define P_EXCOMPAT    ( P_FLAGS & EXCOMPAT_MASK )
  185. #define SHIFTWIDTH_MASK    0x000f
  186. #define ALL_FLAGS_MASK    0x03f0
  187. #define P_APPENDING    (command_giver->interactive->ed_buffer->appending)
  188. #define P_MORE        (command_giver->interactive->ed_buffer->moring)
  189. #define P_LEADBLANKS    (command_giver->interactive->ed_buffer->leading_blanks)
  190. #define P_CUR_AUTOIND   (ED_BUFFER->cur_autoindent)
  191. #define ED_BUFFER       (command_giver->interactive->ed_buffer)
  192.  
  193.  
  194. char    inlin[MAXLINE];
  195. char    *inptr;        /* tty input buffer */
  196. struct ed_buffer {
  197.     int    diag;        /* diagnostic-output? flag */
  198.     int    truncflg;    /* truncate long line flag */
  199.     int    nonascii;    /* count of non-ascii chars read */
  200.     int    nullchar;    /* count of null chars read */
  201.     int    truncated;    /* count of lines truncated */
  202.     char    fname[MAXFNAME];
  203.     int    fchanged;    /* file-changed? flag */
  204.     int    nofname;
  205.     int    mark['z'-'a'+1];
  206.     regexp    *oldpat;
  207.     
  208.     LINE    Line0;
  209.     int    CurLn;
  210.     LINE    *CurPtr;    /* CurLn and CurPtr must be kept in step */
  211.     int    LastLn;
  212.     int    Line1, Line2, nlines;
  213.     int    flags;
  214. #if 0
  215.     int    eightbit;    /* save eighth bit */
  216.     int    nflg;        /* print line number flag */
  217.     int    lflg;        /* print line in verbose mode */
  218.     int    pflg;        /* print current line after each command */
  219. #endif
  220.     int    appending;
  221.     int     moring;         /* used for the wait line of help */
  222.     char    *exit_fn;    /* Function to be called when player exits */
  223.     struct object *exit_ob; /* in this object */
  224.     int    shiftwidth;
  225.     int    leading_blanks;
  226.     int    cur_autoindent;
  227. };
  228.  
  229. struct tbl {
  230.     char    *t_str;
  231.     int    t_and_mask;
  232.     int    t_or_mask;
  233. } *t, tbl[] = {
  234.     "number",    ~FALSE,        NFLG_MASK,
  235.     "nonumber",    ~NFLG_MASK,    FALSE,
  236.     "list",        ~FALSE,        LFLG_MASK,
  237.     "nolist",    ~LFLG_MASK,    FALSE,
  238.     "print",    ~FALSE,     PFLG_MASK,
  239.     "noprint",    ~PFLG_MASK,    FALSE,
  240.     "eightbit",    ~FALSE,        EIGHTBIT_MASK,
  241.     "noeightbit",    ~EIGHTBIT_MASK,    FALSE,
  242.     "autoindent",    ~FALSE,        AUTOINDFLG_MASK,
  243.     "noautoindent",    ~AUTOINDFLG_MASK, FALSE,
  244.     "excompatible", ~FALSE,        EXCOMPAT_MASK,
  245.     "noexcompatible",~EXCOMPAT_MASK,FALSE,
  246.     0
  247. };
  248.  
  249.  
  250. /*-------------------------------------------------------------------------*/
  251.  
  252. #ifndef _AIX
  253. extern    char    *strcpy(), *strncpy();
  254. #endif
  255. extern    char    *xalloc();
  256. extern    LINE    *getptr();
  257. extern    char    *gettxt();
  258. extern    char    *gettxtl();
  259. extern    char    *catsub();
  260. extern    void    prntln(), putcntl(), error();
  261. regexp    *optpat();
  262.  
  263.  
  264. /*________  Macros  ________________________________________________________*/
  265.  
  266. #ifndef max
  267. #  define max(a,b)    ((a) > (b) ? (a) : (b))
  268. #endif
  269.  
  270. #ifndef min
  271. #  define min(a,b)    ((a) < (b) ? (a) : (b))
  272. #endif
  273.  
  274. #define nextln(l)    ((l)+1 > P_LASTLN ? 0 : (l)+1)
  275. #define prevln(l)    ((l)-1 < 0 ? P_LASTLN : (l)-1)
  276.  
  277. #define gettxtl(lin)    ((lin)->l_buff)
  278. #define gettxt(num)    (gettxtl( getptr(num) ))
  279.  
  280. #define getnextptr(p)    ((p)->l_next)
  281. #define getprevptr(p)    ((p)->l_prev)
  282.  
  283. #define setCurLn( lin )    ( P_CURPTR = getptr( P_CURLN = (lin) ) )
  284. #define nextCurLn()    ( P_CURLN = nextln(P_CURLN), P_CURPTR = getnextptr( P_CURPTR ) )
  285. #define prevCurLn()    ( P_CURLN = prevln(P_CURLN), P_CURPTR = getprevptr( P_CURPTR ) )
  286.  
  287. #define clrbuf()    del(1, P_LASTLN)
  288.  
  289. #define    Skip_White_Space    {while (*inptr==SP || *inptr==HT) inptr++;}
  290.  
  291. #define relink(a, x, y, b) { (x)->l_prev = (a); (y)->l_next = (b); }
  292.  
  293.  
  294. /*________  functions  ______________________________________________________*/
  295.  
  296.  
  297. /*    append.c    */
  298.  
  299.  
  300. int append(line, glob)
  301. int    line, glob;
  302. {
  303.     if(glob)
  304.         return(ERR);
  305.     setCurLn( line );
  306.     P_APPENDING = 1;
  307.     if(P_NFLG)
  308.         add_message("%6d. ",P_CURLN+1);
  309.     if (P_CUR_AUTOIND)
  310.         add_message("%*s",P_LEADBLANKS,"");
  311.     set_prompt("*\b");
  312.     return 0;
  313. }
  314.  
  315. int more_append(str)
  316.     char *str;
  317. {
  318.     if(str[0] == '.' && str[1] == '\0') {
  319.         P_APPENDING = 0;
  320.         set_prompt(":");
  321.         return(0);
  322.     }
  323.     if(P_NFLG)
  324.         add_message("%6d. ",P_CURLN+2);
  325.     if ( P_CUR_AUTOIND )
  326.     {
  327.         int i;
  328.         int less_indent_flag = 0;
  329.  
  330.         while ( *str=='\004' || *str == '\013' )
  331.         {
  332.             str++;
  333.             P_LEADBLANKS-=P_SHIFTWIDTH;
  334.             if ( P_LEADBLANKS < 0 ) P_LEADBLANKS=0;
  335.             less_indent_flag=1;
  336.         }
  337.         for ( i=0; i < P_LEADBLANKS; ) inlin[i++]=' ';
  338.         strncpy(inlin+P_LEADBLANKS,str,MAXLINE-P_LEADBLANKS);
  339.         inlin[MAXLINE-1]='\0';
  340.         _count_blanks(inlin,0);
  341.         add_message("%*s",P_LEADBLANKS,"");
  342.         if ( !*str && less_indent_flag ) return 0;
  343.         str=inlin;
  344.     }
  345.     if( ins(str) < 0)
  346.         return( MEM_FAIL );
  347.     return 0;
  348. }
  349.  
  350. static void count_blanks(line)
  351.     int line;
  352. {
  353.     _count_blanks(gettxtl(getptr(line)), 0);
  354. }
  355.  
  356. static void _count_blanks(str,blanks)
  357.     char *str;
  358.     int blanks;
  359. {
  360.     for ( ; *str; str++ )
  361.     {
  362.         if ( *str == ' ' ) blanks++;
  363.         else if ( *str == '\t' ) blanks += 8 - blanks % 8 ;
  364.         else break;
  365.     }
  366.     P_LEADBLANKS = blanks<MAXLINE ? blanks : MAXLINE ;
  367. }
  368.  
  369. /*    ckglob.c    */
  370.  
  371. int ckglob()
  372. {
  373.     regexp    *glbpat;
  374.     char    c, delim, *lin;
  375.     int    num;
  376.     LINE    *ptr;
  377.  
  378.     c = *inptr;
  379.  
  380.     if(c != 'g' && c != 'v')
  381.         return(0);
  382.     if (deflt(1, P_LASTLN) < 0)
  383.         return(ERR);
  384.  
  385.     delim = *++inptr;
  386.     if(delim <= ' ')
  387.         return(ERR);
  388.  
  389.     glbpat = optpat();
  390.     if(*inptr == delim)
  391.         inptr++;
  392.     ptr = getptr(1);
  393.     for (num=1; num<=P_LASTLN; num++) {
  394.         ptr->l_stat &= ~LGLOB;
  395.         if (P_LINE1 <= num && num <= P_LINE2) {
  396.             /* we might have got a NULL pointer if the
  397.                supplied pattern was invalid           */
  398.             if (glbpat) {
  399.                 lin = gettxtl(ptr);
  400.                 if(regexec(glbpat, lin )) {
  401.                     if (c=='g') ptr->l_stat |= LGLOB;
  402.                 } else {
  403.                     if (c=='v') ptr->l_stat |= LGLOB;
  404.                 }
  405.             }
  406.         ptr = getnextptr(ptr);
  407.         }
  408.     }
  409.     return(1);
  410. }
  411.  
  412.  
  413. /*  deflt.c
  414.  *    Set P_LINE1 & P_LINE2 (the command-range delimiters) if the file is
  415.  *    empty; Test whether they have valid values.
  416.  */
  417.  
  418. int deflt(def1, def2)
  419. int    def1, def2;
  420. {
  421.     if(P_NLINES == 0) {
  422.         P_LINE1 = def1;
  423.         P_LINE2 = def2;
  424.     }
  425.     return ( (P_LINE1>P_LINE2 || P_LINE1<=0) ? ERR : 0 );
  426. }
  427.  
  428.  
  429. /*    del.c    */
  430.  
  431. /* One of the calls to this function tests its return value for an error
  432.  * condition.  But del doesn't return any error value, and it isn't obvious
  433.  * to me what errors might be detectable/reportable.  To silence a warning
  434.  * message, I've added a constant return statement. -- RAM
  435.  * ... It could check to<=P_LASTLN ... igp
  436.  */
  437.  
  438. int del(from, to)
  439. int    from, to;
  440. {
  441.     LINE    *first, *last, *next, *tmp;
  442.  
  443.     if(from < 1)
  444.         from = 1;
  445.     first = getprevptr( getptr( from ) );
  446.     last = getnextptr( getptr( to ) );
  447.     next = first->l_next;
  448.     while(next != last && next != &P_LINE0) {
  449.         tmp = next->l_next;
  450.         free((char *)next);
  451.         next = tmp;
  452.     }
  453.     relink(first, last, first, last);
  454.     P_LASTLN -= (to - from)+1;
  455.     setCurLn( prevln(from) );
  456.     return(0);
  457. }
  458.  
  459.  
  460. int dolst(line1, line2)
  461. int line1, line2;
  462. {
  463.     int oldflags=P_FLAGS, p;
  464.  
  465.     P_FLAGS |= LFLG_MASK;
  466.     p = doprnt(line1, line2);
  467.     P_FLAGS = oldflags;
  468.     return p;
  469. }
  470.  
  471.  
  472. /*    esc.c
  473.  * Map escape sequences into their equivalent symbols.  Returns the
  474.  * correct ASCII character.  If no escape prefix is present then s
  475.  * is untouched and *s is returned, otherwise **s is advanced to point
  476.  * at the escaped character and the translated character is returned.
  477.  */
  478. int esc(s)
  479. char    **s;
  480. {
  481.     register int    rval;
  482.  
  483.     if (**s != ESCAPE) {
  484.         rval = **s;
  485.     } else {
  486.         (*s)++;
  487.         switch(islower(**s) ? toupper(**s) : **s) {
  488.         case '\000':
  489.             rval = ESCAPE;    break;
  490.         case 'S':
  491.             rval = ' ';    break;
  492.         case 'N':
  493.             rval = '\n';    break;
  494.         case 'T':
  495.             rval = '\t';    break;
  496.         case 'B':
  497.             rval = '\b';    break;
  498.         case 'R':
  499.             rval = '\r';    break;
  500.         default:
  501.             rval = **s;    break;
  502.         }
  503.     }
  504.     return (rval);
  505. }
  506.  
  507.  
  508. /*    doprnt.c    */
  509.  
  510. int doprnt(from, to)
  511. int    from, to;
  512. {
  513.     from = (from < 1) ? 1 : from;
  514.     to = (to > P_LASTLN) ? P_LASTLN : to;
  515.  
  516.     if(to != 0) {
  517.         setCurLn( from );
  518.         while( P_CURLN <= to ) {
  519.             prntln( gettxtl( P_CURPTR ), P_LFLG, (P_NFLG ? P_CURLN : 0));
  520.             if( P_CURLN == to )
  521.                 break;
  522.             nextCurLn();
  523.         }
  524.     }
  525.     return(0);
  526. }
  527.  
  528.  
  529. void prntln(str, vflg, lin)
  530. char    *str;
  531. int    vflg, lin;
  532. {
  533.     if(lin)
  534.         add_message("%7d ",lin);
  535.     while(*str && *str != NL) {
  536.         if(*str < ' ' || *str >= 0x7f) {
  537.             switch(*str) {
  538.             case '\t':
  539.                 if(vflg)
  540.                     putcntl(*str);
  541.                 else
  542.                     add_message("%c", *str);
  543.                 break;
  544.  
  545.             case DEL:
  546.                 add_message("^?");
  547.                 break;
  548.  
  549.             default:
  550.                 putcntl(*str);
  551.                 break;
  552.             }
  553.         } else
  554.             add_message("%c", *str);
  555.         str++;
  556.     }
  557.     if(vflg)
  558.         add_message("$");
  559.     add_message("\n");
  560. }
  561.  
  562.  
  563. void putcntl(c)
  564. char    c;
  565. {
  566.     add_message("^%c",(c&31)|'@');
  567. }
  568.  
  569.  
  570. /*    egets.c    */
  571.  
  572. int egets(str,size,stream)
  573. char    *str;
  574. int    size;
  575. FILE    *stream;
  576. {
  577.     int    c, count;
  578.     char    *cp;
  579.  
  580.     for(count = 0, cp = str; size > count;) {
  581.         c = getc(stream);
  582.         if(c == EOF) {
  583.             *cp = EOS;
  584.             if(count)
  585.                 add_message("[Incomplete last line]\n");
  586.             return(count);
  587.         }
  588.         else if(c == NL) {
  589.             *cp = EOS;
  590.             return(++count);
  591.         }
  592.         else if (c == 0)
  593.             P_NULLCHAR++;    /* count nulls */
  594.         else {
  595.             if(c > 127) {
  596.                 if(!P_EIGHTBIT)        /* if not saving eighth bit */
  597.                     c = c&127;    /* strip eigth bit */
  598.                 P_NONASCII++;        /* count it */
  599.             }
  600.             *cp++ = c;    /* not null, keep it */
  601.             count++;
  602.         }
  603.     }
  604.     str[count-1] = EOS;
  605.     if(c != NL) {
  606.         add_message("truncating line\n");
  607.         P_TRUNCATED++;
  608.         while((c = getc(stream)) != EOF)
  609.             if(c == NL)
  610.                 break;
  611.     }
  612.     return(count);
  613. }  /* egets */
  614.  
  615.  
  616. int doread(lin, fname)
  617. int    lin;
  618. char    *fname;
  619. {
  620.     FILE    *fp;
  621.     int    err;
  622.     unsigned long    bytes;
  623.     unsigned int    lines;
  624.     static char    str[MAXLINE];
  625.  
  626.     err = 0;
  627.     P_NONASCII = P_NULLCHAR = P_TRUNCATED = 0;
  628.  
  629.     if (P_DIAG) add_message("\"%s\" ",fname);
  630.     if( (fp = fopen(fname, "r")) == NULL ) {
  631.         add_message(" isn't readable.\n");
  632.         return( ERR );
  633.     }
  634.     setCurLn( lin );
  635.     for(lines = 0, bytes = 0;(err = egets(str,MAXLINE,fp)) > 0;) {
  636.         bytes += err;
  637.         if(ins(str) < 0) {
  638.             err = MEM_FAIL;
  639.             break;
  640.         }
  641.         lines++;
  642.     }
  643.     fclose(fp);
  644.     if(err < 0)
  645.         return(err);
  646.     if (P_DIAG) {
  647.         add_message("%u lines %u bytes",lines,bytes);
  648.         if(P_NONASCII)
  649.             add_message(" [%d non-ascii]",P_NONASCII);
  650.         if(P_NULLCHAR)
  651.             add_message(" [%d nul]",P_NULLCHAR);
  652.         if(P_TRUNCATED)
  653.             add_message(" [%d lines truncated]",P_TRUNCATED);
  654.         add_message("\n");
  655.     }
  656.     return( err );
  657. }  /* doread */
  658.  
  659.  
  660. int dowrite(from, to, fname, apflg)
  661. int    from, to;
  662. char    *fname;
  663. int    apflg;
  664. {
  665.     FILE    *fp;
  666.     int    lin, err;
  667.     unsigned int    lines;
  668.     unsigned long    bytes;
  669.     char    *str;
  670.     LINE    *lptr;
  671.  
  672.     err = 0;
  673.     lines = bytes = 0;
  674.  
  675.     add_message("\"%s\" ",fname);
  676.     if((fp = fopen(fname,(apflg?"a":"w"))) == NULL) {
  677.         add_message(" can't be opened for writing!\n");
  678.         return( ERR );
  679.     }
  680.  
  681.     lptr = getptr(from);
  682.     for(lin = from; lin <= to; lin++) {
  683.         str = lptr->l_buff;
  684.         lines++;
  685.         bytes += strlen(str) + 1;    /* str + '\n' */
  686.         if(fputs(str, fp) == EOF) {
  687.             add_message("file write error\n");
  688.             err++;
  689.             break;
  690.         }
  691.         fputc('\n', fp);
  692.         lptr = lptr->l_next;
  693.     }
  694.     add_message("%u lines %lu bytes\n",lines,bytes);
  695.     fclose(fp);
  696.     return( err );
  697. }  /* dowrite */
  698.  
  699.  
  700. /*    find.c    */
  701.  
  702. int find(pat, dir)
  703. regexp    *pat;
  704. int    dir;
  705. {
  706.     int    i, num;
  707.     LINE    *lin;
  708.  
  709.         dir ? nextCurLn() : prevCurLn() ;
  710.     num = P_CURLN;
  711.     lin = P_CURPTR;
  712.     /* if the typed in pattern was invalid we have a NULL pointer! */
  713.     if (!pat) return ( ERR );
  714.     for(i=0; i<P_LASTLN; i++ ) {
  715.         if(regexec( pat, gettxtl( lin ) ))
  716.             return(num);
  717.         if( dir )
  718.             num = nextln(num), lin = getnextptr(lin);
  719.         else
  720.             num = prevln(num), lin = getprevptr(lin);
  721.     }
  722.     return ( ERR );
  723. }
  724.  
  725. #if 0
  726. /*    findg.c by Ted Gaunt for global searches....    much like 'grep' 
  727.     especially useful when line numbering is turned on.
  728. */
  729. int findg(pat, dir)
  730. regexp    *pat;
  731. int    dir;
  732. {
  733.     int    i, num,count;
  734.     LINE    *lin;
  735.     
  736.     count=0;
  737.     num = P_CURLN;
  738.     lin = P_CURPTR;
  739.     for(i=0; i<P_LASTLN; i++ ) {
  740.     if(regexec( pat, gettxtl( lin ) ))
  741.         {prntln( gettxtl( lin ), P_LFLG, (P_NFLG ? P_CURLN : 0));
  742.          count++;}
  743.     if( dir )
  744.         num = nextln(num), lin = getnextptr(lin);
  745.     else
  746.         num = prevln(num), lin = getprevptr(lin);
  747.     nextCurLn();
  748.     }
  749.     if (!count)
  750.     return ( ERR );
  751.     else return (count);
  752. }
  753. #endif /* 0 */
  754.  
  755. /*    getfn.c    */
  756.  
  757. char *getfn(writeflg)
  758. int writeflg;
  759. {
  760.     static char    file[MAXFNAME];
  761.     char    *cp;
  762.     char *file2;
  763. #ifndef COMPAT_MODE
  764.     struct svalue *ret;
  765. #endif
  766.     
  767.     if(*inptr == NL) {
  768.     P_NOFNAME=TRUE;
  769.     file[0] = '/';
  770.     strcpy(file+1, P_FNAME);
  771.     } else {
  772.     P_NOFNAME=FALSE;
  773.     Skip_White_Space;
  774.     
  775.     cp = file;
  776.     while(*inptr && *inptr != NL && *inptr != SP && *inptr != HT)
  777.         *cp++ = *inptr++;
  778.     *cp = '\0';
  779.     
  780.     }
  781.     if(strlen(file) == 0) {
  782.     add_message("bad file name\n");
  783.     return( NULL );
  784.     }
  785. #ifdef COMPAT_MODE
  786.     file2 = check_file_name(file, writeflg);
  787. #else    
  788.     if (file[0] != '/') {
  789.     push_string(file, STRING_MALLOC);
  790.     ret = apply_master_ob("make_path_absolute", 1);
  791.     if (ret == 0 || ret->type != T_STRING)
  792.         return NULL;
  793.     strncpy(file, ret->u.string, sizeof file - 1);
  794.     }
  795.     file2 = check_valid_path(file, command_giver->eff_user,
  796.                  "ed_start", writeflg);
  797. #endif
  798.     if (!file2)
  799.         return( NULL );
  800.     strncpy(file, file2, MAXFNAME-1);
  801.     file[MAXFNAME-1] = 0;
  802.  
  803.     if(strlen(file) == 0) {
  804.         add_message("no file name\n");
  805.         return(NULL);
  806.     }
  807.     return( file );
  808. }  /* getfn */
  809.  
  810.  
  811. int getnum(first)
  812. int first;
  813. {
  814.     regexp    *srchpat;
  815.     int    num;
  816.     char    c;
  817.  
  818.     Skip_White_Space;
  819.  
  820.     if(*inptr >= '0' && *inptr <= '9') {    /* line number */
  821.         for(num = 0; *inptr >= '0' && *inptr <= '9'; ++inptr) {
  822.             num = (num * 10) + (*inptr - '0');
  823.         }
  824.         return num;
  825.     }
  826.  
  827.     switch(c = *inptr) {
  828.     case '.':
  829.         inptr++;
  830.         return (P_CURLN);
  831.  
  832.     case '$':
  833.         inptr++;
  834.         return (P_LASTLN);
  835.  
  836.     case '/':
  837.     case '?':
  838.         srchpat = optpat();
  839.         if(*inptr == c)
  840.             inptr++;
  841.         return(find(srchpat,c == '/'?1:0));
  842.  
  843. #if 0
  844.       case '^':            /* for grep-like searching */
  845.       case '&':
  846.     srchpat = optpat();
  847.     if(*inptr == c)
  848.         inptr++;
  849.     return(findg(srchpat,c == '^'?1:0));
  850. #endif
  851.     
  852.     case '-':
  853.     case '+':
  854.         return(first ? P_CURLN : 1);
  855.  
  856.     case '\'':
  857.         inptr++;
  858.         if (*inptr < 'a' || *inptr > 'z')
  859.             return(EOF);
  860.         return P_MARK[ *inptr++ - 'a' ];
  861.  
  862.     default:
  863.         return ( first ? EOF : 1 );    /* unknown address */
  864.     }
  865. }  /* getnum */
  866.  
  867.  
  868. /*  getone.c
  869.  *    Parse a number (or arithmetic expression) off the command line.
  870.  */
  871. #define FIRST 1
  872. #define NOTFIRST 0
  873.  
  874. int getone()
  875. {
  876.     int    c, i, num;
  877.  
  878.     if((num = getnum(FIRST)) >= 0) {
  879.         for (;;) {
  880.             Skip_White_Space;
  881.             if(*inptr != '+' && *inptr != '-')
  882.                 break;    /* exit infinite loop */
  883.  
  884.                         c = *inptr++;
  885.             if((i = getnum(NOTFIRST)) < 0)
  886.                 return ( i );
  887.             if(c == '+')
  888.                 num += i;
  889.             else
  890.                 num -= i;
  891.         }
  892.     }
  893.     return ( num>P_LASTLN ? ERR : num );
  894. }  /* getone */
  895.  
  896.  
  897. int getlst()
  898. {
  899.     int    num;
  900.  
  901.     P_LINE2 = 0;
  902.     for(P_NLINES = 0; (num = getone()) >= 0;)
  903.     {
  904.         P_LINE1 = P_LINE2;
  905.         P_LINE2 = num;
  906.         P_NLINES++;
  907.         if(*inptr != ',' && *inptr != ';')
  908.             break;
  909.         if(*inptr == ';')
  910.             setCurLn( num );
  911.         inptr++;
  912.     }
  913.     P_NLINES = min(P_NLINES, 2);
  914.     if(P_NLINES == 0)
  915.         P_LINE2 = P_CURLN;
  916.     if(P_NLINES <= 1)
  917.         P_LINE1 = P_LINE2;
  918.  
  919.     return ( (num == ERR) ? num : P_NLINES );
  920. }  /* getlst */
  921.  
  922.  
  923. /*    getptr.c    */
  924.  
  925. LINE *getptr(num)
  926. int    num;
  927. {
  928.     LINE    *ptr;
  929.     int    j;
  930.  
  931.     if (2*num>P_LASTLN && num<=P_LASTLN) {    /* high line numbers */
  932.         ptr = P_LINE0.l_prev;
  933.         for (j = P_LASTLN; j>num; j--)
  934.             ptr = ptr->l_prev;
  935.     } else {                /* low line numbers */
  936.         ptr = &P_LINE0;
  937.         for(j = 0; j < num; j++)
  938.             ptr = ptr->l_next;
  939.     }
  940.     return(ptr);
  941. }
  942.  
  943.  
  944. /*    getrhs.c    */
  945.  
  946. int getrhs(sub)
  947. char    *sub;
  948. {
  949.     char delim = *inptr++;
  950.     char *outmax = sub + MAXPAT;
  951.     if( delim == NL || *inptr == NL)    /* check for eol */
  952.         return( ERR );
  953.     while( *inptr != delim && *inptr != NL ) {
  954.         if ( sub > outmax )
  955.             return ERR;
  956.         if ( *inptr == ESCAPE ) {
  957.             switch ( *++inptr ) {
  958.             case 'r':
  959.                 *sub++ = '\r';
  960.                 inptr++;
  961.                 break;
  962. #if 0
  963.             case ESCAPE:
  964.                 *sub++ = ESCAPE;
  965.                 *sub++ = ESCAPE;
  966.                 inptr++;
  967. #endif
  968.             case 'n':
  969.                 *sub++ = '\n';
  970.                 inptr++;
  971.                 break;
  972.             case 'b':
  973.                 *sub++ = '\b';
  974.                 inptr++;
  975.                 break;
  976.             case 't':
  977.                 *sub++ = '\t';
  978.                 inptr++;
  979.                 break;
  980.             case '0': {
  981.                 int i=3;
  982.                 *sub = 0;
  983.                 do {
  984.                     if (*++inptr<'0' || *inptr >'7')
  985.                         break;
  986.                     *sub = (*sub<<3) | (*inptr-'0');
  987.                 } while (--i!=0);
  988.                 sub++;
  989.                 } break;
  990. #if 0
  991.             default:
  992.                 if ( *inptr != delim )
  993.                     *sub++ = ESCAPE;
  994. #else
  995.             case '&':
  996.             case '1':
  997.             case '2':
  998.             case '3':
  999.             case '4':
  1000.             case '5':
  1001.             case '6':
  1002.             case '7':
  1003.             case '8':
  1004.             case '9':
  1005.             case '\\':
  1006.                 *sub++ = ESCAPE; /* fall through */
  1007.             default:
  1008. #endif
  1009.                 *sub++ = *inptr;
  1010.                 if ( *inptr != NL )
  1011.                     inptr++;
  1012.             }
  1013.         }
  1014.         else *sub++ = *inptr++;
  1015.     }
  1016.     *sub = '\0';
  1017.  
  1018.     inptr++;        /* skip over delimter */
  1019.     Skip_White_Space;
  1020.     if(*inptr == 'g') {
  1021.         inptr++;
  1022.         return( 1 );
  1023.     }
  1024.     return( 0 );
  1025. }
  1026.  
  1027. /*    ins.c    */
  1028.  
  1029. int ins(str)
  1030. char    *str;
  1031. {
  1032.     char    *cp;
  1033.     LINE    *new, *nxt;
  1034.     int    len;
  1035.  
  1036.     do {
  1037.         for ( cp = str; *cp && *cp != NL; cp++ )
  1038.             ;
  1039.         len = cp - str;
  1040.         /* cp now points to end of first or only line */
  1041.  
  1042.         if((new = (LINE *)xalloc(sizeof(LINE)+len)) == NULL)
  1043.             return( MEM_FAIL );     /* no memory */
  1044.  
  1045.         new->l_stat=0;
  1046.         strncpy(new->l_buff,str,len);    /* build new line */
  1047.         new->l_buff[len] = EOS;
  1048.         nxt = getnextptr(P_CURPTR);    /* get next line */
  1049.         relink(P_CURPTR, new, new, nxt);    /* add to linked list */
  1050.         relink(new, nxt, P_CURPTR, new);
  1051.         P_LASTLN++;
  1052.         P_CURLN++;
  1053.         P_CURPTR = new;
  1054.         str = cp + 1;
  1055.     }
  1056.         while( *cp != EOS );
  1057.     return 1;
  1058. }
  1059.  
  1060.  
  1061. /*    join.c    */
  1062.  
  1063. int join(first, last)
  1064. int first, last;
  1065. {
  1066.     char buf[MAXLINE];
  1067.     char *cp=buf, *str;
  1068.     LINE *lin;
  1069.     int num;
  1070.  
  1071.     if (first<=0 || first>last || last>P_LASTLN)
  1072.         return(ERR);
  1073.     if (first==last) {
  1074.         setCurLn( first );
  1075.         return 0;
  1076.     }
  1077.     lin = getptr(first);
  1078.     for (num=first; num<=last; num++) {
  1079.         str=gettxtl(lin);
  1080.         while ( *str ) {
  1081.             if (cp >= buf + MAXLINE-1 ) {
  1082.                 add_message("line too long\n");
  1083.                 return(ERR);
  1084.             }
  1085.             *cp++ = *str++;
  1086.         }
  1087.         lin = getnextptr(lin);
  1088.     }
  1089.     *cp = EOS;
  1090.     del(first, last);
  1091.     if( ins(buf) < 0 )
  1092.         return MEM_FAIL;
  1093.     P_FCHANGED = TRUE;
  1094.     return 0;
  1095. }
  1096.  
  1097.  
  1098. /*  move.c
  1099.  *    Unlink the block of lines from P_LINE1 to P_LINE2, and relink them
  1100.  *    after line "num".
  1101.  */
  1102.  
  1103. int move(num)
  1104. int    num;
  1105. {
  1106.     int    range;
  1107.     LINE    *before, *first, *last, *after;
  1108.  
  1109.     if( P_LINE1 <= num && num <= P_LINE2 )
  1110.         return( ERR );
  1111.     range = P_LINE2 - P_LINE1 + 1;
  1112.     before = getptr(prevln(P_LINE1));
  1113.     first = getptr(P_LINE1);
  1114.     last = getptr(P_LINE2);
  1115.     after = getptr(nextln(P_LINE2));
  1116.  
  1117.     relink(before, after, before, after);
  1118.     P_LASTLN -= range;    /* per AST's posted patch 2/2/88 */
  1119.     if (num > P_LINE1)
  1120.         num -= range;
  1121.  
  1122.     before = getptr(num);
  1123.     after = getptr(nextln(num));
  1124.     relink(before, first, last, after);
  1125.     relink(last, after, before, first);
  1126.     P_LASTLN += range;    /* per AST's posted patch 2/2/88 */
  1127.     setCurLn( num + range );
  1128.     return( 1 );
  1129. }
  1130.  
  1131.  
  1132. int transfer(num)
  1133. int num;
  1134. {
  1135.     int mid, lin, ntrans;
  1136.  
  1137.     if (P_LINE1<=0 || P_LINE1>P_LINE2)
  1138.         return(ERR);
  1139.  
  1140.     mid= num<P_LINE2 ? num : P_LINE2;
  1141.  
  1142.     setCurLn( num );
  1143.     ntrans=0;
  1144.  
  1145.     for (lin=P_LINE1; lin<=mid; lin++) {
  1146.         if( ins(gettxt(lin)) < 0 )
  1147.             return MEM_FAIL;
  1148.         ntrans++;
  1149.     }
  1150.     lin+=ntrans;
  1151.     P_LINE2+=ntrans;
  1152.  
  1153.     for ( ; lin <= P_LINE2; lin += 2 ) {
  1154.         if( ins(gettxt(lin)) < 0 )
  1155.             return MEM_FAIL;
  1156.         P_LINE2++;
  1157.     }
  1158.     return(1);
  1159. }
  1160.  
  1161.  
  1162. /*    optpat.c    */
  1163.  
  1164. regexp *optpat()
  1165. {
  1166.     char    delim, str[MAXPAT], *cp;
  1167.  
  1168.     delim = *inptr++;
  1169.     if (delim == NL)
  1170.         return P_OLDPAT;
  1171.     cp = str;
  1172.     while(*inptr != delim && *inptr != NL && *inptr != EOS && cp < str + MAXPAT - 1) {
  1173.         if(*inptr == ESCAPE && inptr[1] != NL)
  1174.             *cp++ = *inptr++;
  1175.         *cp++ = *inptr++;
  1176.     }
  1177.  
  1178.     *cp = EOS;
  1179.     if(*str == EOS)
  1180.         return(P_OLDPAT);
  1181.     if(P_OLDPAT)
  1182.         free((char *)P_OLDPAT);
  1183.     return P_OLDPAT = regcomp(str,P_EXCOMPAT);
  1184. }
  1185.  
  1186. /* regerror.c */
  1187. void regerror( s )
  1188.     char *s;
  1189. {
  1190.     add_message("ed: %s\n", s );
  1191. }
  1192.  
  1193.  
  1194. int set()
  1195. {
  1196.     char    word[80];
  1197.     int    i;
  1198.  
  1199.     if(*(++inptr) != 't') {
  1200.         if(*inptr != SP && *inptr != HT && *inptr != NL)
  1201.             return(ERR);
  1202.     } else
  1203.         inptr++;
  1204.  
  1205.     if ( (*inptr == NL))
  1206.     {
  1207.         add_message("ed version %d.%d\n", version/100, version%100);
  1208.         for(t = tbl; t->t_str; t+=2) {
  1209.             add_message(    "%s:%s ",t->t_str,
  1210.                 P_FLAGS & t->t_or_mask ?"ON":"OFF");
  1211.         }
  1212.         add_message("\nshiftwidth:%d\n",P_SHIFTWIDTH);
  1213.         return(0);
  1214.     }
  1215.  
  1216.     Skip_White_Space;
  1217.     for(i = 0; *inptr != SP && *inptr != HT && *inptr != NL;) {
  1218.         if (i == sizeof word - 1) {
  1219.         add_message("Too long argument to 'set'!\n");
  1220.         return 0;
  1221.         }
  1222.         word[i++] = *inptr++;
  1223.     }
  1224.     word[i] = EOS;
  1225.     for(t = tbl; t->t_str; t++) {
  1226.         if(strcmp(word,t->t_str) == 0) {
  1227.             P_FLAGS = P_FLAGS & t->t_and_mask | t->t_or_mask;
  1228.             return(0);
  1229.         }
  1230.     }
  1231.     if ( !strcmp(word,"save") ) {
  1232.         struct svalue *ret;
  1233.         push_object(command_giver);
  1234.         push_number( P_SHIFTWIDTH | P_FLAGS );
  1235.         ret = apply_master_ob("save_ed_setup",2);
  1236.         if ( ret && ret->type==T_NUMBER && ret->u.number > 0 )
  1237.             return 0;
  1238.     }
  1239.     if ( !strcmp(word,"shiftwidth") ) {
  1240.         Skip_White_Space;
  1241.         if ( isdigit(*inptr) ) {
  1242.             P_SHIFTWIDTH = *inptr-'0';
  1243.             return 0;
  1244.         }
  1245.     }
  1246.     return SET_FAIL;
  1247. }
  1248.  
  1249. #ifndef relink
  1250. void relink(a, x, y, b)
  1251. LINE    *a, *x, *y, *b;
  1252. {
  1253.     x->l_prev = a;
  1254.     y->l_next = b;
  1255. }
  1256. #endif
  1257.  
  1258.  
  1259. void set_ed_buf()
  1260. {
  1261.     relink(&P_LINE0, &P_LINE0, &P_LINE0, &P_LINE0);
  1262.     P_CURLN = P_LASTLN = 0;
  1263.     P_CURPTR = &P_LINE0;
  1264. }
  1265.  
  1266.  
  1267. /*    subst.c    */
  1268.  
  1269. int subst(pat, sub, gflg, pflag)
  1270. regexp    *pat;
  1271. char    *sub;
  1272. int    gflg, pflag;
  1273. {
  1274.     int    nchngd = 0;
  1275.     char    *txtptr;
  1276.     char    *new, *old, buf[MAXLINE];
  1277.     int    space;            /* amylaar */
  1278.     int    still_running = 1;
  1279.     LINE    *lastline = getptr( P_LINE2 );
  1280.  
  1281.     if(P_LINE1 <= 0)
  1282.         return( SUB_FAIL );
  1283.     nchngd = 0;        /* reset count of lines changed */
  1284.  
  1285.     for( setCurLn( prevln( P_LINE1 ) ); still_running; ) {
  1286.         nextCurLn();
  1287.         new = buf;
  1288.         space = MAXLINE;    /* amylaar */
  1289.         if ( P_CURPTR == lastline )
  1290.             still_running = 0;
  1291.         if ( regexec( pat, txtptr = gettxtl( P_CURPTR ) ) ) {
  1292.             do
  1293.                 {
  1294.                 /* Copy leading text */
  1295.                 int diff = pat->startp[0] - txtptr;
  1296.                 if ( (space-=diff) < 0 )    /* amylaar */
  1297.                     return SUB_FAIL;
  1298.                 strncpy( new, txtptr, diff );
  1299.                 new += diff;
  1300.                 /* Do substitution */
  1301.                 old = new;
  1302.                 new = regsub( pat, sub, new, space);
  1303.                 if (!new || (space-= new-old) < 0) /* amylaar */
  1304.                     return SUB_FAIL;
  1305.                 if (txtptr == pat->endp[0]) { /* amylaar :
  1306.                                        prevent infinite loop */
  1307.                     if ( !*txtptr ) break;
  1308.                     if (--space < 0) return SUB_FAIL;
  1309.                     *new++ = *txtptr++;
  1310.                 } else
  1311.                     txtptr = pat->endp[0];
  1312.                 }
  1313.             while( gflg && !pat->reganch && regexec( pat, txtptr ));
  1314.  
  1315.             /* Copy trailing chars */
  1316.             /* amylaar : always check for enough space left
  1317.              * BEFORE altering memory
  1318.              */
  1319.             if ( (space-= strlen(txtptr)+1 ) < 0 )
  1320.                 return SUB_FAIL;
  1321.             strcpy(new, txtptr);
  1322.             del(P_CURLN,P_CURLN);
  1323.             if( ins(buf) < 0 )
  1324.                 return MEM_FAIL;
  1325.             nchngd++;
  1326.             if(pflag)
  1327.                 doprnt(P_CURLN, P_CURLN);
  1328.         }
  1329.         }
  1330.     return (( nchngd == 0 && !gflg ) ? SUB_FAIL : nchngd);
  1331. }
  1332. #define MAX_INDENT 40
  1333. int indent[MAX_INDENT];
  1334. char codes[MAX_INDENT];
  1335. int ind_occur[MAX_INDENT];
  1336. int str_on;
  1337. int indent_level;
  1338. int temp_indent;
  1339. int indent_error;
  1340.  
  1341. static int indent_code() {
  1342.     int from,to,current;
  1343.     char *inlip;
  1344.     /* static char    locti[MAXLINE]; */
  1345.     /* static char idented[MAXLINE]; */
  1346.     from=1;
  1347.     to= P_LASTLN;
  1348.     
  1349.     indent_level=0;
  1350.     str_on=0;
  1351.     temp_indent=0;
  1352.     indent_error=0;
  1353.     for (current=0;current<MAX_INDENT;current++) {
  1354.     indent[current]=0;
  1355.     codes[current]=SP;
  1356.     ind_occur[current]=0;}
  1357.     P_FCHANGED = TRUE;
  1358.     for (current=from;current<=to;current++) 
  1359.     {
  1360.     setCurLn(current);
  1361.     inlip=gettxtl( P_CURPTR );
  1362.     strip_buff(current,inlip);
  1363.     if (indent_error) return (ERR);
  1364.     }
  1365.     return 0;
  1366. }
  1367.  
  1368. static int strip_buff(line,buff2)
  1369.     int line;
  1370.     char *buff2;
  1371. {
  1372.     int i;
  1373.     int i2;
  1374.     int flag, flagnotif;
  1375.     static char buff[MAXLINE];
  1376.     for (i=0;i<(indent_level+temp_indent)*3;i++) buff[i]=SP;
  1377.     i=(indent_level+temp_indent)*3;
  1378.     flag=flagnotif=0;
  1379.     temp_indent=0;
  1380.     for (i2=0;i2<MAXLINE;i2++) {
  1381.         switch (buff2[i2]) {
  1382.           case PP:
  1383.             if (buff2[i2-1]!='\\') str_on=!str_on;
  1384.             flag=1;
  1385.             buff[i++]=buff2[i2];
  1386.             break;
  1387.           case EOL:
  1388.             if (buff2[i2-1]!='\\') {
  1389.                 if (str_on) {add_message("Detected a unterminated string on line %d\n",line);indent_error=1;}
  1390.                 str_on=0;
  1391.         
  1392.             }
  1393.             buff[i++]=MAXLINE;
  1394.             i2=1000;
  1395.             break;
  1396.           case SP:
  1397.             if (flag) buff[i++]=buff2[i2];
  1398.             break;
  1399.           case TAB:
  1400.             if (str_on) buff[i++]=buff2[i2];
  1401.             break;
  1402.           case NL:
  1403.             flag=0;
  1404.             if (buff2[i2-1]!='\\') {
  1405.                 if (str_on) {
  1406.             add_message("Detected a unterminated string on line %d\n",line);
  1407.             indent_error=1;}
  1408.                 str_on=0;
  1409.             }
  1410.             buff[i++]=buff2[i2];
  1411.             break;
  1412.           case LB:
  1413.             flag=1;
  1414.             if (!str_on) {
  1415.                 codes[++indent_level]=buff[i++]=buff2[i2];
  1416.         ind_occur[indent_level]=line;
  1417.                 if (indent_level==1) indent[1]=3;
  1418.             } else buff[i++]=buff2[i2];
  1419.         flagnotif=1;
  1420.         temp_indent=0;
  1421.             break;
  1422.       case ';':
  1423.         flag=1;
  1424.         buff[i++]=buff2[i2];
  1425.         flagnotif=1;
  1426.         temp_indent=0;
  1427.         break;
  1428.           case LC:
  1429.           case LS:
  1430.             flag=1;
  1431.             if (!str_on) {
  1432.                 codes[++indent_level]=buff[i++]=buff2[i2];
  1433.         ind_occur[indent_level]=line;
  1434.                 indent[indent_level]=i;
  1435.             } else buff[i++]=buff2[i2];
  1436.             break;
  1437.           case RB:
  1438.             if (!str_on) {
  1439.         if (LB!=codes[indent_level]) {
  1440.             add_message("Mismatched brackets, '%c' on line %d\n",
  1441.                 buff2[i2],line);
  1442.             add_message("-which doesn't match '%c' on line %d\n",
  1443.                 codes[indent_level],ind_occur[indent_level]);
  1444.             indent_error=1;}
  1445.                 indent_level--;
  1446.                 if (indent_level<0) {
  1447.                     indent_level=0;}
  1448.         if (!flag && i) i=indent[indent_level];
  1449.                 buff[i++]=buff2[i2];
  1450.             } else buff[i++]=buff2[i2];
  1451.             flag=1;
  1452. /*        flagnotif=1; */
  1453.             break;
  1454.           case RC:
  1455.             if (!str_on) {
  1456.         if (LC!=codes[indent_level]) {
  1457.             add_message("Mismatched brackets, '%c' on line %d\n",
  1458.                 buff2[i2],line);
  1459.             add_message("-which doesn't match '%c' on line %d\n",
  1460.                 codes[indent_level],ind_occur[indent_level]);
  1461.             indent_error=1;}
  1462.                 indent_level--;
  1463.                 if (indent_level<0) {
  1464.                     indent_level=0;}
  1465.         if (!flag && i) i=indent[indent_level];
  1466.                 buff[i++]=buff2[i2];
  1467.             } else buff[i++]=buff2[i2];
  1468.             flag=1;
  1469.         /*        flagnotif=1;*/
  1470.             break;
  1471.           case RS:
  1472.             if (!str_on) {
  1473.         if (LS!=codes[indent_level]) {
  1474.             add_message("Mismatched brackets, '%c' on line %d\n",
  1475.                 buff2[i2],line);
  1476.             add_message("-which doesn't match '%c' on line %d\n",
  1477.                 codes[indent_level],ind_occur[indent_level]);
  1478.             indent_error=1;}
  1479.                 indent_level--;
  1480.                 if (indent_level<0) {
  1481.                     indent_level=0;}
  1482.         if (!flag && i) i=indent[indent_level];
  1483.                 buff[i++]=buff2[i2];
  1484.             } else buff[i++]=buff2[i2];
  1485.             flag=1;
  1486.         /*        flagnotif=1;*/
  1487.             break;
  1488.       case '#':
  1489.         if (!flag) i=0;
  1490.         flag=1;
  1491.             buff[i++]=buff2[i2];
  1492.             break;
  1493.       case 'i':
  1494.         if (!str_on && buff2[i2+1]=='f') {temp_indent=1; flagnotif=0;}
  1495.         if (flagnotif) {temp_indent=0; flagnotif=0;}
  1496.             flag=1;
  1497.             buff[i++]=buff2[i2];
  1498.             break;
  1499.       case 'e':
  1500.         if (!str_on && (i2+10)<MAXLINE)
  1501.         if (buff2[i2+1]=='l' && buff2[i2+2]=='s' && buff2[i2+3]=='e') 
  1502.         {temp_indent=1; flagnotif=0; indent[indent_level+1]=i+3;}
  1503.         /*drop through*/
  1504.           default:
  1505.         if (flagnotif) {temp_indent=0; flagnotif=0;}
  1506.             flag=1;
  1507.             buff[i++]=buff2[i2];
  1508.             break;
  1509.         }
  1510.     }
  1511.     del(line,line);
  1512.     ins(buff);
  1513.     return 1;
  1514. }
  1515.  
  1516. /*  docmd.c
  1517.  *    Perform the command specified in the input buffer, as pointed to
  1518.  *    by inptr.  Actually, this finds the command letter first.
  1519.  */
  1520.  
  1521. int docmd(glob)
  1522. int    glob;
  1523. {
  1524.     static char    rhs[MAXPAT];
  1525.     regexp    *subpat;
  1526.     int    c, err, line3;
  1527.     int    apflg, pflag, gflag;
  1528.     int    nchng;
  1529.     char    *fptr;
  1530.  
  1531.     pflag = FALSE;
  1532.     Skip_White_Space;
  1533.  
  1534.     c = *inptr++;
  1535.     switch(c) {
  1536.     case NL:
  1537.         if( P_NLINES == 0 && (P_LINE2 = nextln(P_CURLN)) == 0 )
  1538.             return(ERR);
  1539.         setCurLn( P_LINE2 );
  1540.         return (1);
  1541.  
  1542.     case '=':
  1543.         add_message("%d\n",P_LINE2);
  1544.         break;
  1545.  
  1546.     case 'a':
  1547.     case 'A':
  1548.         P_CUR_AUTOIND = c=='a' ? P_AUTOINDFLG : !P_AUTOINDFLG;
  1549.         if(*inptr != NL || P_NLINES > 1)
  1550.             return(ERR);
  1551.  
  1552.         if ( P_CUR_AUTOIND ) count_blanks(P_LINE1);
  1553.         if(append(P_LINE1, glob) < 0)
  1554.             return(ERR);
  1555.         P_FCHANGED = TRUE;
  1556.         break;
  1557.  
  1558.     case 'c':
  1559.         if(*inptr != NL)
  1560.             return(ERR);
  1561.  
  1562.         if(deflt(P_CURLN, P_CURLN) < 0)
  1563.             return(ERR);
  1564.  
  1565.         P_CUR_AUTOIND = P_AUTOINDFLG;
  1566.         if ( P_AUTOINDFLG ) count_blanks(P_LINE1);
  1567.         if(del(P_LINE1, P_LINE2) < 0)
  1568.             return(ERR);
  1569.         if(append(P_CURLN, glob) < 0)
  1570.             return(ERR);
  1571.         P_FCHANGED = TRUE;
  1572.         break;
  1573.  
  1574.     case 'd':
  1575.         if(*inptr != NL)
  1576.             return(ERR);
  1577.  
  1578.         if(deflt(P_CURLN, P_CURLN) < 0)
  1579.             return(ERR);
  1580.  
  1581.         if(del(P_LINE1, P_LINE2) < 0)
  1582.             return(ERR);
  1583.         if(nextln(P_CURLN) != 0)
  1584.             nextCurLn();
  1585.         P_FCHANGED = TRUE;
  1586.         break;
  1587.  
  1588.     case 'e':
  1589.         if(P_NLINES > 0)
  1590.             return(ERR);
  1591.         if(P_FCHANGED)
  1592.             return CHANGED;
  1593.         /*FALL THROUGH*/
  1594.     case 'E':
  1595.         if(P_NLINES > 0)
  1596.             return(ERR);
  1597.  
  1598.         if(*inptr != ' ' && *inptr != HT && *inptr != NL)
  1599.             return(ERR);
  1600.  
  1601.         if((fptr = getfn(0)) == NULL)
  1602.             return(ERR);
  1603.  
  1604.         clrbuf();
  1605.         (void)doread(0, fptr);
  1606.  
  1607.         strcpy(P_FNAME, fptr);
  1608.         P_FCHANGED = FALSE;
  1609.         break;
  1610.  
  1611.     case 'f':
  1612.         if(P_NLINES > 0)
  1613.             return(ERR);
  1614.  
  1615.         if(*inptr != ' ' && *inptr != HT && *inptr != NL)
  1616.             return(ERR);
  1617.  
  1618.         fptr = getfn(0);
  1619.  
  1620.         if (P_NOFNAME)
  1621.             add_message("%s\n", P_FNAME);
  1622.         else {
  1623.             if(fptr == NULL) return(ERR);
  1624.             strcpy(P_FNAME, fptr);
  1625.         }
  1626.         break;
  1627.  
  1628.     case 'i':
  1629.         if(*inptr != NL || P_NLINES > 1)
  1630.             return(ERR);
  1631.  
  1632.         P_CUR_AUTOIND = P_AUTOINDFLG;
  1633.         if ( P_AUTOINDFLG ) count_blanks(P_LINE1);
  1634.         if(append(prevln(P_LINE1), glob) < 0)
  1635.             return(ERR);
  1636.         P_FCHANGED = TRUE;
  1637.         break;
  1638.  
  1639.     case 'j':
  1640.         if (*inptr != NL || deflt(P_CURLN, P_CURLN+1)<0)
  1641.             return(ERR);
  1642.  
  1643.         if (join(P_LINE1, P_LINE2) < 0)
  1644.             return(ERR);
  1645.         break;
  1646.  
  1647.     case 'k':
  1648.         Skip_White_Space;
  1649.  
  1650.         if (*inptr < 'a' || *inptr > 'z')
  1651.             return ERR;
  1652.         c= *inptr++;
  1653.  
  1654.         if(*inptr != ' ' && *inptr != HT && *inptr != NL)
  1655.             return(ERR);
  1656.  
  1657.         P_MARK[c-'a'] = P_LINE1;
  1658.         break;
  1659.  
  1660.     case 'l':
  1661.         if(*inptr != NL)
  1662.             return(ERR);
  1663.         if(deflt(P_CURLN,P_CURLN) < 0)
  1664.             return(ERR);
  1665.         if (dolst(P_LINE1,P_LINE2) < 0)
  1666.             return(ERR);
  1667.         break;
  1668.  
  1669.     case 'm':
  1670.         if((line3 = getone()) < 0)
  1671.             return(ERR);
  1672.         if(deflt(P_CURLN,P_CURLN) < 0)
  1673.             return(ERR);
  1674.         if(move(line3) < 0)
  1675.             return(ERR);
  1676.         P_FCHANGED = TRUE;
  1677.         break;
  1678.  
  1679.       case 'n':
  1680.     P_FLAGS = P_FLAGS & ~NFLG_MASK & ~LFLG_MASK
  1681.         | ( NFLG_MASK | LFLG_MASK ) & !P_NFLG ;
  1682.     P_DIAG=!P_DIAG;
  1683.     add_message(    "number %s, list %s\n",
  1684.             P_NFLG?"ON":"OFF",
  1685.             P_LFLG?"ON":"OFF");
  1686.     break;
  1687.         
  1688.       case 'I':
  1689.     if(P_NLINES > 0)
  1690.         return(ERR);
  1691.     if(*inptr != NL)
  1692.         return(ERR);
  1693.     add_message("Indenting entire code...\n");
  1694.     if (indent_code())
  1695.         add_message("Indention halted.\n");
  1696.     else 
  1697.         add_message("Done indenting.\n");
  1698.     break;
  1699.  
  1700.       case 'H':
  1701.       case 'h': 
  1702.     print_help(*(inptr++));
  1703.     break;
  1704.  
  1705.     case 'P':
  1706.     case 'p':
  1707.         if(*inptr != NL)
  1708.             return(ERR);
  1709.         if(deflt(P_CURLN,P_CURLN) < 0)
  1710.             return(ERR);
  1711.         if(doprnt(P_LINE1,P_LINE2) < 0)
  1712.             return(ERR);
  1713.         break;
  1714.  
  1715.     case 'q':
  1716.         if(P_FCHANGED)
  1717.             return CHANGED;
  1718.         /*FALL THROUGH*/
  1719.     case 'Q':
  1720.         clrbuf();
  1721.         if(*inptr == NL && P_NLINES == 0 && !glob)
  1722.             return(EOF);
  1723.         else
  1724.             return(ERR);
  1725.  
  1726.     case 'r':
  1727.         if(P_NLINES > 1)
  1728.             return(ERR);
  1729.  
  1730.         if(P_NLINES == 0)        /* The original code tested */
  1731.             P_LINE2 = P_LASTLN;    /*    if(P_NLINES = 0)    */
  1732.                         /* which looks wrong.  RAM  */
  1733.  
  1734.         if(*inptr != ' ' && *inptr != HT && *inptr != NL)
  1735.             return(ERR);
  1736.  
  1737.         if((fptr = getfn(0)) == NULL)
  1738.             return(ERR);
  1739.  
  1740.         if((err = doread(P_LINE2, fptr)) < 0)
  1741.             return(err);
  1742.         P_FCHANGED = TRUE;
  1743.         break;
  1744.  
  1745.     case 's':
  1746.         if(*inptr == 'e')
  1747.             return(set());
  1748.         Skip_White_Space;
  1749.         if((subpat = optpat()) == NULL)
  1750.             return(ERR);
  1751.         if((gflag = getrhs(rhs)) < 0)
  1752.             return(ERR);
  1753.         if(*inptr == 'p')
  1754.             pflag++;
  1755.         if(deflt(P_CURLN, P_CURLN) < 0)
  1756.             return(ERR);
  1757.         if((nchng = subst(subpat, rhs, gflag, pflag)) < 0)
  1758.             return(ERR);
  1759.         if(nchng)
  1760.             P_FCHANGED = TRUE;
  1761.         if ( nchng==1 && P_PFLG ) {
  1762.             if(doprnt(P_CURLN, P_CURLN) < 0)
  1763.             return(ERR);
  1764.         }
  1765.         break;
  1766.  
  1767.     case 't':
  1768.         if((line3 = getone()) < 0)
  1769.             return(ERR);
  1770.         if(deflt(P_CURLN,P_CURLN) < 0)
  1771.             return(ERR);
  1772.         if(transfer(line3) < 0)
  1773.             return(ERR);
  1774.         P_FCHANGED = TRUE;
  1775.         break;
  1776.  
  1777.     case 'W':
  1778.     case 'w':
  1779.         apflg = (c=='W');
  1780.  
  1781.         if(*inptr != ' ' && *inptr != HT && *inptr != NL)
  1782.             return(ERR);
  1783.  
  1784.         if((fptr = getfn(1)) == NULL)
  1785.             return(ERR);
  1786.  
  1787.         if(deflt(1, P_LASTLN) < 0)
  1788.             return(ERR);
  1789.         if(dowrite(P_LINE1, P_LINE2, fptr, apflg) < 0)
  1790.             return(ERR);
  1791.         P_FCHANGED = FALSE;
  1792.         break;
  1793.  
  1794.     case 'x':
  1795.         if(*inptr == NL && P_NLINES == 0 && !glob) {
  1796.             if((fptr = getfn(1)) == NULL)
  1797.                 return(ERR);
  1798.             if(dowrite(1, P_LASTLN, fptr, 0) >= 0)
  1799.                 return(EOF);
  1800.         }
  1801.         return(ERR);
  1802.  
  1803.     case 'z':
  1804.         if(deflt(P_CURLN,P_CURLN) < 0)
  1805.             return(ERR);
  1806.  
  1807.         switch(*inptr) {
  1808.         case '-':
  1809.             if(doprnt(P_LINE1-21,P_LINE1) < 0)
  1810.                 return(ERR);
  1811.             break;
  1812.  
  1813.         case '.':
  1814.             if(doprnt(P_LINE1-11,P_LINE1+10) < 0)
  1815.                 return(ERR);
  1816.             break;
  1817.  
  1818.         case '+':
  1819.         case '\n':
  1820.             if(doprnt(P_LINE1,P_LINE1+21) < 0)
  1821.                 return(ERR);
  1822.             break;
  1823.         }
  1824.         break;
  1825.  
  1826.       case 'Z':
  1827.     if(deflt(P_CURLN,P_CURLN) < 0)
  1828.         return(ERR);
  1829.     
  1830.     switch(*inptr) {
  1831.       case '-':
  1832.         if(doprnt(P_LINE1-41,P_LINE1) < 0)
  1833.         return(ERR);
  1834.         break;
  1835.         
  1836.       case '.':
  1837.         if(doprnt(P_LINE1-21,P_LINE1+20) < 0)
  1838.         return(ERR);
  1839.         break;
  1840.         
  1841.       case '+':
  1842.       case '\n':
  1843.         if(doprnt(P_LINE1,P_LINE1+41) < 0)
  1844.         return(ERR);
  1845.         break;
  1846.     }
  1847.     break;    
  1848.     default:
  1849.         return(ERR);
  1850.     }
  1851.  
  1852.     return (0);
  1853. }  /* docmd */
  1854.  
  1855.  
  1856. /*    doglob.c    */
  1857. int doglob()
  1858. {
  1859.     int    lin, status;
  1860.     char    *cmd;
  1861.     LINE    *ptr;
  1862.  
  1863.     cmd = inptr;
  1864.  
  1865.     for (;;) {
  1866.         ptr = getptr(1);
  1867.         for (lin=1; lin<=P_LASTLN; lin++) {
  1868.             if (ptr->l_stat & LGLOB)
  1869.                 break;
  1870.             ptr = getnextptr(ptr);
  1871.         }
  1872.         if (lin > P_LASTLN)
  1873.             break;
  1874.  
  1875.         ptr->l_stat &= ~LGLOB;
  1876.         P_CURLN = lin; P_CURPTR = ptr;
  1877.         inptr = cmd;
  1878.         if((status = getlst()) < 0)
  1879.             return(status);
  1880.         if((status = docmd(1)) < 0)
  1881.             return(status);
  1882.     }
  1883.     return(P_CURLN);
  1884. }  /* doglob */
  1885.  
  1886.  
  1887. /*
  1888.  * Start the editor. Because several players can edit simultaneously,
  1889.  * they will each need a separate editor data block.
  1890.  *
  1891.  * If an exit_fn and exit_ob is given, then call exit_ob->exit_fn at
  1892.  * exit of editor. The purpose is to make it possible for external LPC
  1893.  * code to maintain a list of locked files.
  1894.  */
  1895. void ed_start(file_arg, exit_fn, exit_ob)
  1896.     char *file_arg;
  1897.     char *exit_fn;
  1898.     struct object *exit_ob;
  1899. {
  1900.     struct svalue *setup;
  1901.     if (!command_giver->interactive)
  1902.         error("Tried to start an ed session on a non-interative player.\n");
  1903.     if (ED_BUFFER)
  1904.         error("Tried to start an ed session, when already active.\n");
  1905. #ifdef COMPAT_MODE
  1906.     if (command_giver != current_object)
  1907.         error("Illegal start of ed.\n");
  1908. #endif
  1909.     ED_BUFFER = (struct ed_buffer *)xalloc(sizeof (struct ed_buffer));
  1910.     memset((char *)command_giver->interactive->ed_buffer, '\0',
  1911.            sizeof (struct ed_buffer));
  1912.     ED_BUFFER->truncflg = 1;
  1913.     ED_BUFFER->flags |= EIGHTBIT_MASK;
  1914.     ED_BUFFER->shiftwidth= 4;
  1915.     push_object(command_giver);
  1916.     setup = apply_master_ob("retrieve_ed_setup",1);
  1917.     if ( setup && setup->type==T_NUMBER && setup->u.number ) {
  1918.         ED_BUFFER->flags      = setup->u.number & ALL_FLAGS_MASK;
  1919.         ED_BUFFER->shiftwidth = setup->u.number & SHIFTWIDTH_MASK;
  1920.     }
  1921.     ED_BUFFER->CurPtr =
  1922.         &command_giver->interactive->ed_buffer->Line0;
  1923.     if (exit_fn) {
  1924.         ED_BUFFER->exit_fn = string_copy(exit_fn);
  1925.         exit_ob->ref ++ ;
  1926.     } else {
  1927.         ED_BUFFER->exit_fn = 0;
  1928.     }
  1929.     ED_BUFFER->exit_ob = exit_ob;
  1930.     set_ed_buf();
  1931.  
  1932.     /*
  1933.      * Check for read on startup, since the buffer is read in. But don't
  1934.      * check for write, since we may want to change the file name.
  1935.      * When in compatibility mode, we assume that the test of valid read
  1936.      * is done by the caller of ed().
  1937.      */
  1938.     if(file_arg
  1939. #ifndef COMPAT_MODE
  1940.        && (file_arg =
  1941.            check_valid_path(file_arg, command_giver->eff_user,
  1942.                 "ed_start", 0))
  1943. #endif       
  1944.        && !doread(0, file_arg)) {
  1945.         setCurLn( 1 );
  1946.     }
  1947.     if (file_arg) {
  1948.         strncpy(P_FNAME, file_arg, MAXFNAME-1);
  1949.         P_FNAME[MAXFNAME-1] = 0;
  1950.         add_message("/%s, %d lines\n", file_arg, P_LASTLN);
  1951.     } else {
  1952.         add_message("No file.\n");
  1953.     }
  1954.     set_prompt(":");
  1955.     return;
  1956. }
  1957.  
  1958. static void free_ed_buffer() {
  1959.     clrbuf();
  1960.     if (ED_BUFFER->exit_fn) {
  1961.     char *name;
  1962.     struct object *ob;
  1963.         ob = ED_BUFFER->exit_ob;
  1964.     name = ED_BUFFER->exit_fn;
  1965.         free((char *)ED_BUFFER);
  1966.     ED_BUFFER = 0;
  1967.     set_prompt("> ");
  1968.         apply(name,
  1969.             ob, 0);
  1970.         free(name);
  1971.         free_object(ob, "ed EOF");
  1972.         return;
  1973.     }
  1974.     free((char *)ED_BUFFER);
  1975.     ED_BUFFER = 0;
  1976.     add_message("Exit from ed.\n");
  1977.     set_prompt("> ");
  1978.     return;
  1979. }
  1980.  
  1981. void ed_cmd(str)
  1982.     char *str;
  1983. {
  1984.     int status;
  1985.  
  1986.     if (P_MORE) {
  1987.         print_help2();
  1988.         return;
  1989.     }
  1990.     if (P_APPENDING) {
  1991.         more_append(str);
  1992.         return;
  1993.     }
  1994.     if (strlen(str) < MAXLINE)
  1995.         strcat(str, "\n");
  1996.     
  1997.     strncpy(inlin, str, MAXLINE-1);
  1998.     inlin[MAXLINE-1] = 0;
  1999.     inptr = inlin;
  2000.     if(getlst() >= 0)
  2001.         if((status = ckglob()) != 0) {
  2002.             if(status >= 0 && (status = doglob()) >= 0) {
  2003.                 setCurLn( status );
  2004.                 return;
  2005.             }
  2006.         } else {
  2007.             if((status = docmd(0)) >= 0) {
  2008.                 if(status == 1)
  2009.                     doprnt(P_CURLN, P_CURLN);
  2010.                 return;
  2011.             }
  2012.         }
  2013.     switch (status) {
  2014.     case EOF:
  2015.             free_ed_buffer();
  2016.         return;
  2017.     case FATAL:
  2018.         if (ED_BUFFER->exit_fn) {
  2019.             free(ED_BUFFER->exit_fn);
  2020.             free_object(ED_BUFFER->exit_ob, "ed FATAL");
  2021.         }
  2022.         free((char *)ED_BUFFER);
  2023.         ED_BUFFER= 0;
  2024.         add_message("FATAL ERROR\n");
  2025.         set_prompt("> ");
  2026.         return;
  2027.     case CHANGED:
  2028.         add_message("File has been changed.\n");
  2029.         break;
  2030.     case SET_FAIL:
  2031.         add_message("`set' command failed.\n");
  2032.         break;
  2033.     case SUB_FAIL:
  2034.         add_message("string substitution failed.\n");
  2035.         break;
  2036.     case MEM_FAIL:
  2037.         add_message("Out of memory: text may have been lost.\n" );
  2038.         break;
  2039.     default:
  2040.         add_message("Unrecognized or failed command.\n");
  2041.         /*  Unrecognized or failed command (this  */
  2042.         /*  is SOOOO much better than "?" :-)      */
  2043.     }
  2044. }
  2045.  
  2046. void save_ed_buffer()
  2047. {
  2048.     struct svalue *stmp;
  2049.     char *fname;
  2050.  
  2051.     push_string(P_FNAME, STRING_SHARED);
  2052.     stmp = apply_master_ob("get_ed_buffer_save_file_name",1);
  2053.     if (stmp) {
  2054.     if (stmp->type == T_STRING) {
  2055.         fname = stmp->u.string;
  2056.         if (*fname == '/') fname++;
  2057.             dowrite(1, P_LASTLN, fname , 0);
  2058.     }
  2059.         free_svalue(stmp);
  2060.     }
  2061.     free_ed_buffer();
  2062. }
  2063.  
  2064. static void print_help(arg)
  2065.     int arg;
  2066. {
  2067.     switch (arg) {
  2068.     case 'I':
  2069.     add_message("       Automatic Indentation (V 1.0)\n");
  2070.     add_message("------------------------------------\n");
  2071.     add_message("           by Qixx [Update: 7/10/91]\n");
  2072.     add_message("\nBy using the command 'I', a program is run which will\n");
  2073.     add_message("automatically indent all lines in your code.  As this is\n");
  2074.     add_message("being done, the program will also search for some basic\n");
  2075.     add_message("errors (which don't show up good during compiling) such as\n");
  2076.     add_message("Unterminated String, Mismatched Brackets and Parentheses,\n");
  2077.     add_message("and indented code is easy to understand and debug, since if\n");
  2078.     add_message("your brackets are off -- the code will LOOK wrong. Please\n");
  2079.     add_message("mail me at gaunt@mcs.anl.gov with any pieces of code which\n");
  2080.     add_message("don't get indented properly.\n");
  2081.     break;
  2082. #if 0
  2083.     case '^':
  2084.     add_message("Command: ^   Usage: ^pattern\n");
  2085.     add_message("This command is similiar to grep, in that it searches the\n");
  2086.     add_message("entire file, printing every line that contains the specified\n");
  2087.     add_message("pattern.  To get the line numbers of found lines, turn on line\n");
  2088.     add_message("number printing with the 'n' command.\n");
  2089.     break;
  2090. #endif
  2091.     case 'n':
  2092.     add_message("Command: n   Usage: n\n");
  2093.     add_message("This command toggles the internal flag which will cause line\n");
  2094.     add_message("numbers to be printed whenever a line is listed.\n");
  2095.     break;
  2096.     case 'a':
  2097.     add_message("Command: a   Usage: a\n");
  2098.     add_message("Append causes the editor to enter input mode, inserting all text\n");
  2099.     add_message("starting AFTER the current line. Use a '.' on a blank line to exit\n");
  2100.     add_message("this mode.\n");
  2101.     break;
  2102.     case 'A':
  2103.         add_message("Command: A   Usage: A\n\
  2104. Like the 'a' command, but uses inverse autoindent mode.\n");
  2105.     break;
  2106.     case 'i':
  2107.     add_message("Command: i   Usage: i\n");
  2108.     add_message("Insert causes the editor to enter input mode, inserting all text\n");
  2109.     add_message("starting BEFORE the current line. Use a '.' on a blank line to exit\n");
  2110.     add_message("this mode.\n");
  2111.     break;
  2112.     case 'c':
  2113.     add_message("Command: c   Usage: c\n");
  2114.     add_message("Change command causes the current line to be wiped from memory.\n");
  2115.     add_message("The editor enters input mode and all text is inserted where the previous\n");
  2116.     add_message("line existed.\n");
  2117.     break;
  2118.     case 'd':
  2119.     add_message("Command: d   Usage: d  or [range]d\n");
  2120.     add_message("Deletes the current line unless preceeded with a range of lines,\n");
  2121.     add_message("then the entire range will be deleted.\n");
  2122.     break;
  2123.     case 'e':
  2124.     add_message("Commmand: e  Usage: e filename\n");
  2125.     add_message("Causes the current file to be wiped from memory, and the new file\n");
  2126.     add_message("to be loaded in.\n");
  2127.     break;      
  2128.     case 'E':
  2129.     add_message("Commmand: E  Usage: E filename\n");
  2130.     add_message("Causes the current file to be wiped from memory, and the new file\n");
  2131.     add_message("to be loaded in.  Different from 'e' in the fact that it will wipe\n");
  2132.     add_message("the current file even if there are unsaved modifications.\n");
  2133.     break;
  2134.     case 'f':
  2135.     add_message("Command: f  Usage: f  or f filename\n");
  2136.     add_message("Display or set the current filename.   If  filename is given as \nan argument, the file (f) command changes the current filename to\nfilename; otherwise, it prints  the current filename.\n");
  2137.     break;
  2138.     case 'g':
  2139.     add_message("Command: g  Usage: g/re/p\n");
  2140.     add_message("Search in all lines for expression 're', and print\n");
  2141.     add_message("every match. Command 'l' can also be given\n");
  2142.     add_message("Unlike in unix ed, you can also supply a range of lines\n");
  2143.     add_message("to search in\n");
  2144.     add_message("Compare with command 'v'.\n");
  2145.     break;
  2146.     case 'h':
  2147.     add_message("Command: h    Usage:  h  or hc (where c is a command)\n");
  2148.     add_message("Help files added by Qixx.\n");
  2149.     break;
  2150.     case 'j':
  2151.     add_message("Command: j    Usage: j or [range]j\n");
  2152.     add_message("Join Lines. Remove the NEWLINE character  from  between the  two\naddressed lines.  The defaults are the current line and the line\nfollowing.  If exactly one address is given,  this  command does\nnothing.  The joined line is the resulting current line.\n");
  2153.     break;
  2154.     case 'k':
  2155.     add_message("Command: k   Usage: kc  (where c is a character)\n");
  2156.     add_message("Mark the addressed line with the name c,  a  lower-case\nletter.   The  address-form,  'c,  addresses  the  line\nmarked by c.  k accepts one address; the default is the\ncurrent line.  The current line is left unchanged.\n");
  2157.     break;
  2158.     case 'l':
  2159.     add_message("Command: l   Usage: l  or  [range]l\n");
  2160.     add_message("List the current line or a range of lines in an unambiguous\nway such that non-printing characters are represented as\nsymbols (specifically New-Lines).\n");
  2161.     break;
  2162.     case 'm':
  2163.     add_message("Command: m   Usage: mADDRESS or [range]mADDRESS\n");
  2164.     add_message("Move the current line (or range of lines if specified) to a\nlocation just after the specified ADDRESS.  Address 0 is the\nbeginning of the file and the default destination is the\ncurrent line.\n");
  2165.     break;
  2166.     case 'p':
  2167.     add_message("Command: p    Usage: p  or  [range]p\n");
  2168.     add_message("Print the current line (or range of lines if specified) to the\nscreen. See the command 'n' if line numbering is desired.\n");
  2169.     break;
  2170.     case 'q':
  2171.     add_message("Command: q    Usage: q\n");
  2172.     add_message("Quit the editor. Note that you can't quit this way if there\nare any unsaved changes.  See 'w' for writing changes to file.\n");
  2173.     break;
  2174.     case 'Q':
  2175.     add_message("Command: Q    Usage: Q\n");
  2176.     add_message("Force Quit.  Quit the editor even if the buffer contains unsaved\nmodifications.\n");
  2177.     break;
  2178.     case 'r':
  2179.     add_message("Command: r    Usage: r filename\n");
  2180.     add_message("Reads the given filename into the current buffer starting\nat the current line.\n");
  2181.     break;
  2182.     case 't':
  2183.     add_message("Command: t   Usage: tADDRESS or [range]tADDRESS\n");
  2184.     add_message("Transpose a copy of the current line (or range of lines if specified)\nto a location just after the specified ADDRESS.  Address 0 is the\nbeginning of the file and the default destination\nis the current line.\n");
  2185.     break;
  2186.     case 'v':
  2187.     add_message("Command: v   Usage: v/re/p\n");
  2188.     add_message("Search in all lines without expression 're', and print\n");
  2189.     add_message("every match. Other commands than 'p' can also be given\n");
  2190.     add_message("Compare with command 'g'.\n");
  2191.     break;
  2192.     case 'z':
  2193.     add_message("Command: z   Usage: z  or  z-  or z.\n");
  2194.     add_message("Displays 20 lines starting at the current line.\nIf the command is 'z.' then 20 lines are displayed being\ncentered on the current line. The command 'z-' displays\nthe 20 lines before the current line.\n");
  2195.     break;
  2196.     case 'Z':
  2197.     add_message("Command: Z   Usage: Z  or  Z-  or Z.\n");
  2198.     add_message("Displays 40 lines starting at the current line.\nIf the command is 'Z.' then 40 lines are displayed being\ncentered on the current line. The command 'Z-' displays\nthe 40 lines before the current line.\n");
  2199.     break;
  2200.     case 'x':
  2201.     add_message("Command: x   Usage: x\n");
  2202.     add_message("Save file under the current name, and then exit from ed.\n");
  2203.     break;
  2204.     case 's':
  2205.     if ( *inptr=='e' && *(inptr+1)=='t' ) {
  2206.         add_message("\
  2207. Without arguments: show current settings.\n\
  2208. 'set save' will preserve the current settings for subsequent invocations of ed.\n\
  2209. Options:\n\
  2210. \n\
  2211. number       will print line numbers before printing or inserting a lines\n\
  2212. list       will print control characters in p(rint) and z command like in l(ist)\n\
  2213. print       will show current line after a single substitution\n\
  2214. eightbit\n\
  2215. autoindent will preserve current indentation while entering text.\n\
  2216.        use ^D or ^K to get back one step back to the right.\n\
  2217. excompatible will exchange the meaning of \\( and ( as well as \\) and )\n\
  2218. \n\
  2219. An option can be cleared by prepending it with 'no' in the set command, e.g.\n\
  2220. 'set nolist' to turn off the list option.\n\
  2221. \n\
  2222. set shiftwidth <digit> will store <digit> in the shiftwidth variable, which\n\
  2223. determines how much blanks are removed from the current indentation when\n\
  2224. typing ^D or ^K in the autoindent mode.\n");
  2225.         break;
  2226.     } else ;
  2227. /* is there anyone who wants to add an exact description for the 's' command? */
  2228.     case 'w':
  2229.     case 'W':
  2230.     case '/':
  2231.     case '?':
  2232.     add_message("Sorry no help yet for this command. Try again later.\n");
  2233.     break;
  2234.     default:
  2235.     add_message("       Help for Ed  (V 2.0)\n");
  2236.     add_message("---------------------------------\n");
  2237.     add_message("     by Qixx [Update: 7/10/91]\n");
  2238.     add_message("\n\nCommands\n--------\n");
  2239.     add_message("/\tsearch forward for pattern\n");
  2240.     add_message("?\tsearch backward for a pattern\n");
  2241.     /* add_message("^\tglobal search and print for pattern\n"); */
  2242.     add_message("=\tshow current line number\n");
  2243.     add_message("a\tappend text starting after this line\n");
  2244.     add_message("A\tlike 'a' but with inverse autoindent mode\n"),
  2245.     add_message("c\tchange current line, query for replacement text\n");
  2246.     add_message("d\tdelete line(s)\n");
  2247.     add_message("e\treplace this file with another file\n");
  2248.     add_message("E\tsame as 'e' but works if file has been modified\n");
  2249.     add_message("f\tshow/change current file name\n");
  2250.     add_message("g\tSearch and execute command on any matching line.\n");
  2251.     add_message("h\thelp file (display this message)\n");
  2252.     add_message("i\tinsert text starting before this line\n");
  2253.     add_message("I\tindent the entire code (Qixx version 1.0)\n");
  2254.     add_message("\n--Return to continue--");
  2255.     P_MORE=1;
  2256.     break;
  2257.     }
  2258. }
  2259.  
  2260. static void print_help2() {
  2261.     P_MORE=0;
  2262.     add_message("j\tjoin lines together\n");
  2263.     add_message("k\tmark this line with a character - later referenced as 'a\n");
  2264.     add_message("l\tline line(s) with control characters displayed\n");
  2265.     add_message("m\tmove line(s) to specified line\n");
  2266.     add_message("n\ttoggle line numbering\n");
  2267.     add_message("p\tprint line(s) in range\n");
  2268.     add_message("q\tquit editor\n");
  2269.     add_message("Q\tquit editor even if file modified and not saved\n\
  2270. r\tread file into editor at end of file or behind the given line\n");
  2271.     add_message("s\tsearch and replace\n");
  2272.     add_message("set\tquery, change or save option settings\n");
  2273.     add_message("t\tmove copy of line(s) to specified line\n");
  2274.     add_message("v\tSearch and execute command on any non-matching line.\n");
  2275.     add_message("x\tsave file and quit\n");
  2276.     add_message("w\twrite to current file (or specified file)\n");
  2277.     add_message("W\tlike the 'w' command but appends instead\n");
  2278.     add_message("z\tdisplay 20 lines, possible args are . + -\n");
  2279.     add_message("Z\tdisplay 40 lines, possible args are . + -\n");
  2280.     add_message("\nFor further information type 'hc' where c is the command\nthat help is desired for.\n");
  2281. }
  2282.